from copy import deepcopy
from random import randint
from functools import reduce
import math

#检验一个列表中的所有元素是否都一样
def all_equal(lst):
  return lst[1:] == lst[:-1]
all_equal([1, 2, 3, 4, 5, 6]) # False
all_equal([1, 1, 1, 1]) # True

# 如果列表所有值都是唯一的，返回 True，否则 False
def all_unique(lst):
  return len(lst) == len(set(lst))

# 将列表值分组。如果在filter的元素是True，那么对应的元素属于第一个组；否则属于第二个组
def bifurcate(lst, filter):
  return [
    [x for i,x in enumerate(lst) if filter[i] == True],
    [x for i,x in enumerate(lst) if filter[i] == False]
  ]
bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])

# 返回两个iterables间的差异
def difference(a, b):
  _b = set(b)
  return [item for item in a if item not in _b]
difference([1, 2, 3], [1, 2, 4]) # [3]
difference([1, 2, 4,5,7], [1, 5, 4]) #[2,7]

# 一次性的整合列表
def flatten(lst):
  return [x for y in lst for x in y]
flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8]

# 将一个数分解转换为个位数字
def digitize(n):
  return list(map(int, str(n)))
digitize(123) # [1, 2, 3]

# 将列表元素顺序随机打乱
def shuffle(lst):
  temp_lst = deepcopy(lst)
  m = len(temp_lst)
  while (m):
    m -= 1
    i = randint(0, m)
    temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
  return temp_lst
foo = [1,2,3,4,5,6]

# 将数字num钳在由a和b边界值规定的范围中
def clamp_number(num,a,b):
  return max(min(num, max(a,b)),min(a,b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1
clamp_number(4, 3, 6) #4

# 返回字符串的字节数
def byte_size(string):
  return len(string.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11

# 计算几个数的最大公因数
def gcd(numbers):
  return reduce(math.gcd, numbers)
gcd([8,36,28]) # 4

if __name__ == '__main__':
    print(gcd([8,32,16]))
